package com.dean.scoket;

import cn.hutool.json.JSONBeanParser;
import cn.hutool.json.JSONObject;
import com.dean.netty.entity.DeanNettySendMsg;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Map;

public class DeanScoketServerReceiveMsg implements Runnable{

    private Socket client;
    private DataInputStream dis;

    public DeanScoketServerReceiveMsg(Socket client){
        this.client = client;
        try {
            // 接受客户端消息
            dis = new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void run() {
        while(true){
            try {
                // 服务端接受到消息 如果有指定发送用户 则需要转发
                String receiveMsg = dis.readUTF();
                ObjectMapper objectMapper = new ObjectMapper();
                DeanSocketMsg socketMsg = objectMapper.readValue(receiveMsg, DeanSocketMsg.class);
                String msgType = socketMsg.getMsgType();
                String fromUser = socketMsg.getFromUser();
                // 登录消息的处理
                if("LOGIN".equals(msgType)){
                    // 则获取登录信息并缓存
                    DeanScoketSever.users.put(fromUser,client);
                    System.out.println("IP"+client.getInetAddress().getHostAddress()+"用户:"+fromUser+"已上线");
                } else if ("TRANS".equals(msgType)) {
                    Boolean sendFlag = socketMsg.getTransFlag();
                    // 如果消息已转发则不在处理
                    if(sendFlag==true){
                        return;
                    }
                    String toUser = socketMsg.getToUser();
                    String content = socketMsg.getContent();
                    System.out.println("接收到客户端["+fromUser+"]转发给客户端["+toUser+"]消息:"+content);
                    Socket toClient = DeanScoketSever.getUsers().get(toUser);
                    // 用户不在线则直接退回消息
                    if(toClient == null){
                        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
                        socketMsg.setContent("用户["+toUser+"]未上线,请稍后重试");
                        socketMsg.setFromUser("admin");
                        socketMsg.setTransFlag(false);
                        dos.writeUTF(objectMapper.writeValueAsString(socketMsg));
                    }else{
                        DataOutputStream dos = new DataOutputStream(toClient.getOutputStream());
                        socketMsg.setTransFlag(true);
                        String transMsg = objectMapper.writeValueAsString(socketMsg);
                        dos.writeUTF(transMsg);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
